package com.xbs.dao.enterprise;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.ParamSetimpl;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.CarEntity;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.EnterpriseModelEntity;
import com.xbs.entity.EnterpriseMotorcadeEntity;
import com.xbs.entity.EnterpriseOrderDetailEntity;
import com.xbs.entity.EnterpriseOrderEntity;
/**
 * 企业首页dao层
 * @author Administrator
 * 插入为save
 * 获取数据为get
 */
public class EnterpriseIndex extends BaseDao{
	//建立车型表行对象
	private RowDatas<CarModelEntity> carmodel;
	//获取车型表数据
	public List<CarModelEntity> getCarModel(String epid){
		//实例化行对象
		carmodel=new RowDatas<CarModelEntity>() {
			@Override
			public CarModelEntity getRowData(ResultSet rs) throws SQLException {
				CarModelEntity cme=new CarModelEntity();
				cme.setCarmodelid(rs.getString("car_model_id"));
				cme.setCarmodelgps(rs.getString("car_model_gps"));
				cme.setCarmodelname(rs.getString("car_model_name"));
				cme.setCarmodelimage(rs.getString("car_model_image"));
				cme.setCarmodelseat(rs.getString("car_model_seat"));
				cme.setCarmodeltype(rs.getString("car_model_type"));
				cme.setCarmodelyear(rs.getString("car_model_year"));
				cme.setDiscount(rs.getDouble("company_discount"));
				cme.setDoornumber(rs.getInt("car_model_door_number"));
				cme.setFueltype("car_model_fuel_type");
				cme.setInsurance(rs.getDouble("car_model_insurance"));
				cme.setRentcash(rs.getDouble("car_model_rent_cash"));
				cme.setCarnum(rs.getInt("carnum"));
				return cme;
			}
		};
		//sql语言
		String sql="select count(m.car_model_id) carnum,m.car_model_id,m.car_model_gps,m.car_model_name,m.car_model_image,m.car_model_seat,m.car_model_type,m.car_model_year,m.company_discount,m.car_model_door_number,m.car_model_fuel_type,m.car_model_insurance,m.car_model_insurance,m.car_model_rent_cash  from tb_car_model m,tb_car c where c.store_id=(select enterprise_store from tb_enterprise where enterprise_id=?) and m.car_model_id=c.car_model_id and c.car_status=0 GROUP BY m.car_model_id ";
		//调用baseDao中queryList方法
		return queryList(sql,new ParamSet(epid),carmodel);
		
	}
	/**
	 * 获取车队信息
	 */
	public List<EnterpriseMotorcadeEntity> getMotorcade(){
		RowDatas<EnterpriseMotorcadeEntity> rd=new RowDatas<EnterpriseMotorcadeEntity>() {

			@Override
			public EnterpriseMotorcadeEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseMotorcadeEntity emce=new EnterpriseMotorcadeEntity();
				emce.setBelongcarmotorcade(rs.getInt("belong_motocade"));
				emce.setCarid(rs.getInt("car_model_id"));
				emce.setPrise(rs.getDouble("car_money"));
				emce.setCarname(rs.getString("car_model_name"));
				emce.setNum(rs.getInt("car_count"));
				emce.setCar_model_door_number(rs.getInt("car_model_door_number"));
				emce.setCar_model_fuel_type(rs.getString("car_model_fuel_type"));
				emce.setCar_model_image(rs.getString("car_model_image"));
				emce.setCar_model_seating(rs.getInt("car_model_seating"));
				emce.setMotorcade_end_time(rs.getDate("motorcade_end_time"));
				emce.setCar_money(rs.getDouble("car_money"));
				emce.setCar_model_rent_cash(rs.getDouble("car_model_rent_cash"));
				
				return emce;
			}
			
		};
		
		String sql="select t2.car_model_id,t2.car_model_name,t1.car_count,t1.car_money,t1.belong_motocade,t2.car_model_door_number,t2.car_model_fuel_type,t2.car_model_image,t2.car_model_seating,t1.motorcade_end_time,t1.car_money,t2.car_model_rent_cash "
				+"from  tb_motorcade t1,tb_car_model t2 "
				+"where t1.car_model_id=t2.car_model_id";
		return queryList(sql, rd);
	}

	
	/**
	 * 将企业订单表对象插入到企业订单表中
	 */
	public void saveEnterpriseOrder(EnterpriseOrderEntity eoe){
		String sql="insert into tb_enterprise_order(eo_account,eo_order_id,eo_reserve_get_time,eo_order_status,eo_rent_type,eo_moto_perday_rent,eo_establish_time) "
				+ "values(?,?,?,?,?,?,?)";
		ParamSet set=new ParamSet(eoe.getEo_account(),eoe.getEo_order_id(),eoe.getEo_reserve_get_time(),eoe.getEo_order_status(),eoe.getEo_rent_type(),eoe.getSumCash(),eoe.getEo_establish_time());
		
		updateExecuted(sql, set);
	}
	/**
	 * 将企业订单车型表对象插入到企业车型表中
	 */
	public void saveEnterpriseModel(EnterpriseModelEntity eme){
		String sql="insert into tb_enterprise_model(eo_order_id,car_model_id,car_model_number,rent_money) values(?,?,?,?)";
		ParamSet set=new ParamSet(eme.getEo_order_id(),eme.getCar_model_id(),eme.getCar_model_number(),eme.getRent_money());
		
		updateExecuted(sql,set);
	}
	/**
	 * 根据一个车型编号获取企业日租价格
	 */
	public Double getRentCashByCarModelId(String id){
		RowDatas<Double> rd=new RowDatas<Double>() {

			@Override
			public Double getRowData(ResultSet rs) throws SQLException {
				Double d=new Double(rs.getDouble("rent"));
				return d;
			}
		};
		String sql="select car_model_rent_cash*company_discount as rent from tb_car_model where car_model_id=?";
		
		return query(sql,new ParamSet(id),rd);
	}
	/**
	 * 根据一组编号获取车型表实体
	 */
	public List<EnterpriseOrderDetailEntity> getCarModelByIds(String[] ids){
		
		RowDatas<EnterpriseOrderDetailEntity> carmodelsbyid=new RowDatas<EnterpriseOrderDetailEntity>() {
			@Override
			public EnterpriseOrderDetailEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseOrderDetailEntity eode=new EnterpriseOrderDetailEntity();
				eode.setCarid(rs.getString("car_model_id"));
				eode.setCarname(rs.getString("car_model_name"));
				eode.setPersonnum(rs.getInt("car_model_seating"));
				eode.setPicurl(rs.getString("car_model_image"));
				eode.setRentCash(rs.getDouble("cash"));
				
				return eode;
			}
		};
		String sql="select m.car_model_id,m.car_model_name,m.car_model_image,m.car_model_seating,m.company_discount*m.car_model_rent_cash as cash"
				+ " from tb_car_model m where";
		StringBuffer sqll=new StringBuffer(sql);
		if(ids.length==0)
		{
			return null;
		}
		for(int i=0;i<ids.length;i++){
			sqll.append(" car_model_id='"+ids[i]+"' or");
		}
		String fsql=sqll.substring(0,sqll.length()-3);
		System.out.println(fsql);
		return queryList(fsql, carmodelsbyid);
	
	}
	/**
	 * 根据车队表所属车队编号查询车的详细信息
	 */
	public List<EnterpriseOrderDetailEntity> getMotoCarById(String id){
		RowDatas<EnterpriseOrderDetailEntity> rd=new RowDatas<EnterpriseOrderDetailEntity>() {
			@Override
			public EnterpriseOrderDetailEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseOrderDetailEntity eode=new EnterpriseOrderDetailEntity();
				eode.setCarid(rs.getString("car_model_id"));
				eode.setCarname(rs.getString("car_model_name"));
				eode.setNum(rs.getInt("car_count"));
				eode.setPersonnum(rs.getInt("car_model_seating"));
				eode.setRentCash(rs.getDouble("car_money"));
				eode.setPicurl(rs.getString("car_model_image"));
				return eode;
			}
		};
		String sql="select c.car_model_id,c.car_model_name,c.car_model_seating,m.car_count,m.car_money,c.car_model_image"
				+ " from tb_motorcade m,tb_car_model c"
				+ " where m.car_model_id=c.car_model_id and m.belong_motocade=?";
		
		return queryList(sql,new ParamSet(Integer.parseInt(id)), rd);
	}
	
}
